package com.easemob.chatuidemo.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.service.UserService;
import com.easemob.chatuidemo.utils.FlyAntGlobalVar;

public class ChangeUserinfoActivity extends Activity {
    /**
     * 输入框
     */
    private EditText et_change_infos;
    /**
     * 标题栏
     */
    private TextView tv_change_infos;
    
    private String information = "";
    private int INFO_NUM;
    private int BACK = 99;
    public static final String INFORMATION = "INFORMATIONS";
    private ProgressDialog progressDialog;
    
    private String name = " ";
    private String nickname = " ";
    private String address = " ";
    private String sendTel= " ";
    private String sendAddress = " ";
    private String comName = " ";
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();
        INFO_NUM = intent.getExtras().getInt("index");
        
        information = intent.getExtras().getString("information");
//        name = intent.getExtras().getString("username");
//        nickname = intent.getExtras().getString("nickname");
//        address = intent.getExtras().getString("address");
//        sendTel = intent.getExtras().getString("sendTel");
//        sendAddress = intent.getExtras().getString("goosdaddress");
        setContentView(R.layout.activity_change_userinfos);
        
        tv_change_infos = (TextView) findViewById(R.id.tv_change_userinfo);
        et_change_infos = (EditText) findViewById(R.id.et_change_userinfo);
//        btn_change_userinfo = (Button) findViewById(R.id.btn_change_userinfo);

        switch (INFO_NUM) {
        case 1:
            tv_change_infos.setText(getString(R.string.personal_name));
            break;
        case 2:
            tv_change_infos.setText(getString(R.string.personal_phonenum));
            break;
        case 3:
            tv_change_infos.setText(getString(R.string.personal_goodsaddress));
            break;
        case 4:
            tv_change_infos.setText(getString(R.string.personal_address));
            break;
        case 5:
            tv_change_infos.setText(getString(R.string.nickname));
            break;
        default:
            break;
        }
        et_change_infos.setText(intent.getExtras().getString("information"));
    }
    
    public void submit(View view){
        SharedPreferences preferences = this.getSharedPreferences("filename", MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        String key = "";
        String value = "";
        switch (INFO_NUM) {
        case 1:
            key = "name";
            name = et_change_infos.getText().toString();
            value = name;
            break;
        case 2:
            key = "sendTel";
            sendTel = et_change_infos.getText().toString();
            value = sendTel;
            break;
        case 3:
            key = "sendAddress";
            sendAddress = et_change_infos.getText().toString();
            value = sendAddress;
            break;
        case 4:
            key = "address";
            address = et_change_infos.getText().toString();
            value = address;
            break;
        case 5:
            key = "nickname";
            nickname = et_change_infos.getText().toString();
            value = nickname;
            break;
        default:
            break;
        }
        new NetWorkTask().execute(uid, key, value);
    }
    public void back(View v) {
        Intent intent=new Intent();
        setResult(BACK, intent);
        this.finish();
    }
    
    
    void progressDialogInit(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(android.R.drawable.ic_menu_info_details);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
        progressDialog.setMessage("正在提交");
    }
    
    class NetWorkTask extends AsyncTask<String, Integer, String>{
        @Override
        protected void onPreExecute() {
            progressDialogInit();
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            UserService uService = new UserService();
            String retCode = uService.updateUserInfos(params[0], params[1],params[2]);
            return retCode;
        }



        @Override
        protected void onPostExecute(String result) {
            if ( result == null) {
                progressDialog.dismiss();
                AlertDialog.Builder builder = new Builder(ChangeUserinfoActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.setMessage("网络连接失败,请检查网络情况");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        }
                    });
                builder.create().show();
                return;
            }
            if ("100".equals(result)) {
                Intent intent=new Intent();
                switch (INFO_NUM) {
                case 1:
                    name = et_change_infos.getText().toString();
                    break;
                case 2:
                    sendTel = et_change_infos.getText().toString();
                    break;
                case 3:
                    sendAddress = et_change_infos.getText().toString();
                    break;
                case 4:
                    address = et_change_infos.getText().toString();
                    break;
                case 5:
                    nickname = et_change_infos.getText().toString();
                    break;
                default:
                    break;
                }
                intent.putExtra("name", name);
                intent.putExtra("nickname", nickname);
                intent.putExtra("sendTel", sendTel);
                intent.putExtra("sendAddress", sendAddress);
                intent.putExtra("address", address);
                setResult(INFO_NUM, intent);
                finish();
            }else{
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
            progressDialog.dismiss();
            super.onPostExecute(result);
        }
        
    }

}
